package org.szgb.console.sys.user.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.department.entity.SysDepartment;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysUser extends BeanEntity<SysUser> {
    @Serial
    private static final long serialVersionUID = 1L;
    private String username;
    private String no;
    private String name;
    private String phone;
    private String password;
    private String token;
    private SysDepartment department;

    public SysUser() {
    }

    public SysUser(String id) {
        this.setId(id);
    }

    public SysUser(SysUserLoginDto sysUserLoginDto) {
        this.setToken(sysUserLoginDto.token);
        this.setUsername(sysUserLoginDto.username);
        this.setPassword(sysUserLoginDto.password);
    }

    public SysUser(SysUserFindDto sysUserFindDto) {
        this.setId(sysUserFindDto.id);
        this.setUsername(sysUserFindDto.username);
        this.setNo(sysUserFindDto.no);
        this.setName(sysUserFindDto.name);
        this.setDepartment(new SysDepartment(sysUserFindDto.departmentId));
        this.setPageDto(sysUserFindDto.pageDto);
    }

    public SysUser(SysUserSaveDto sysUserSaveDto) {
        this.setId(sysUserSaveDto.id);
        this.setUsername(sysUserSaveDto.username);
        this.setNo(sysUserSaveDto.no);
        this.setName(sysUserSaveDto.name);
        this.setPhone(sysUserSaveDto.phone);
        this.setPassword(sysUserSaveDto.password);
        this.setDepartment(new SysDepartment(sysUserSaveDto.departmentId));
    }

    public SysUserLoginView toSysUserLoginView(List<SysUserRole> sysUserRoleList) {
        return new SysUserLoginView(this, sysUserRoleList);
    }

    public SysUserView toSysUserView() {
        return new SysUserView(this);
    }

    @Data
    @Schema(title = "用户信息", description = "用户信息")
    public static class SysUserView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "用户名")
        private String username;
        @Schema(description = "工号")
        private String no;
        @Schema(description = "姓名")
        private String name;
        @Schema(description = "手机号")
        private String phone;
        @Schema(description = "部门ID")
        private String departmentId;
        @Schema(description = "部门名称")
        private String departmentName;
        @Schema(description = "归属线路id")
        private String lineId;
        @Schema(description = "归属线路名称")
        private String lineName;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysUserView(SysUser sysUser) {
            this.id = sysUser.getId();
            this.username = sysUser.getUsername();
            this.no = sysUser.getNo();
            this.name = sysUser.getName();
            this.phone = sysUser.getPhone();
            if (sysUser.getDepartment() != null) {
                this.departmentId = sysUser.getDepartment().getId();
                this.departmentName = sysUser.getDepartment().getName();
                if(sysUser.getDepartment().getSysLine() != null){
                    this.lineId = sysUser.getDepartment().getSysLine().getId();
                    this.lineName = sysUser.getDepartment().getSysLine().getName();
                }
            }
            this.createdAt = sysUser.getCreatedAt();
            this.updatedAt = sysUser.getUpdatedAt();
            if (sysUser.getCreatedBy() != null) {
                this.createdById = sysUser.getCreatedBy().getId();
                this.createdByName = sysUser.getCreatedBy().getName();
            }
            if (sysUser.getUpdatedBy() != null) {
                this.updatedById = sysUser.getUpdatedBy().getId();
                this.updatedByName = sysUser.getUpdatedBy().getName();
            }
        }

        public static List<SysUserView> getViewListFromList(List<SysUser> sysUserList) {
            return sysUserList.stream().map(SysUserView::new).toList();
        }
    }

    @Data
    @Schema(title = "登录信息", description = "用户登录登录")
    public static class SysUserLoginView {
        private String id;
        private String name;
        private String no;
        private String token;
        private String departmentId;
        private String departmentName;
        private String lineId;
        private String lineName;
        private String roleIds;
        private String roleNames;

        public SysUserLoginView(SysUser sysUser, List<SysUserRole> sysUserRoles) {
            this.id = sysUser.getId();
            this.name = sysUser.getName();
            this.no = sysUser.getNo();
            this.token = sysUser.getToken();
            if(sysUser.getDepartment() != null){
            this.departmentId = sysUser.getDepartment().getId();
            this.departmentName = sysUser.getDepartment().getName();
                if (sysUser.getDepartment().getSysLine() != null) {
                    this.lineId = sysUser.getDepartment().getSysLine().getId();
                    this.lineName = sysUser.getDepartment().getSysLine().getName();
                }
            }
            this.roleIds = sysUserRoles.stream().map(item -> item.getRole().getId()).collect(Collectors.joining(","));
            this.roleNames = sysUserRoles.stream().map(item -> item.getRole().getName()).collect(Collectors.joining(","));
        }
    }

    @Data
    @Schema(title = "登录表单", description = "用户登录参数")
    public static class SysUserLoginDto {
        @Schema(description = "token")
        private String token;
        @Schema(description = "用户名")
        private String username;
        @Schema(description = "密码")
        private String password;


        public SysUser toSysUser() {
            return new SysUser(this);
        }
    }

    @Data
    @Schema(title = "用户查询表单", description = "用户查询参数")
    public static class SysUserFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "用户名")
        private String username;
        @Schema(description = "部门ID")
        private String departmentId;
        @Schema(description = "工号")
        private String no;
        @Schema(description = "姓名")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysUser toSysUser() {
            return new SysUser(this);
        }
    }

    @Data
    @Schema(title = "用户保存表单", description = "用户保存参数")
    public static class SysUserSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "用户名")
        private String username;
        @Schema(description = "工号")
        private String no;
        @Schema(description = "姓名")
        private String name;
        @Schema(description = "手机号")
        private String phone;
        @Schema(description = "密码")
        private String password;
        @Schema(description = "部门ID")
        private String departmentId;

        public SysUser toSysUser() {
            return new SysUser(this);
        }
    }

    @Data
    @Schema(title = "用户密码更改表单", description = "用户密码更改参数")
    public static class SysUserChangePasswordDto {
        @Schema(description = "旧密码")
        private String oldPassword;
        @Schema(description = "新密码")
        private String newPassword;
    }
}
